using UnityEngine;
using QFramework;
using DG.Tweening;

namespace ProjectIndieFarm
{
    public partial class CameraController : ViewController
    {
        private Transform mPlayer;

        private bool mMovementEnable = true;

        public static CameraController Instance
        {
            get
            {
                return instance;
            }

            private set
            {
                instance = value;
            }
        }
        private static CameraController instance;

        private void Awake()
        {
            Instance = this;
        }
        void Start()
        {
            mPlayer = FindObjectOfType<Player>().transform;
        }

        private void Update()
        {
            if (mMovementEnable)
            {
                var position = Vector2.Lerp(transform.position, 
                    mPlayer.position, 1 - Mathf.Exp(-Time.deltaTime * 10));
                transform.position = new Vector3(position.x, position.y, transform.position.z);
            }
        }

        private void OnDestroy()
        {
            Instance = null;
        }


        public static void ShakeHeavy()
        {
            Instance.mMovementEnable = false;
            Instance.GetComponent<Camera>()
                .DOShakePosition(0.2f, 0.1f, 100, 180, true, ShakeRandomnessMode.Harmonic)
                .OnComplete(() =>
                {
                    Instance.mMovementEnable = true;
                });
        }
        public static void ShakeMiddle()
        {
            Instance.mMovementEnable = false;
            Instance.GetComponent<Camera>().DOShakePosition(0.2f, 0.05f, 100, 180, true, ShakeRandomnessMode.Harmonic).OnComplete(() =>
            {
                Instance.mMovementEnable = true;
            });
        }
        public static void ShakeSlight()
        {
            Instance.mMovementEnable = false;
            Instance.GetComponent<Camera>().DOShakePosition(0.2f, 0.02f, 100, 180, true, ShakeRandomnessMode.Harmonic).OnComplete(() =>
            {
                Instance.mMovementEnable = true;
            });
        }
    }
}
